<?php

/**
 * 角色管理
 * @author renpper
 * @date 2015-1-4
 */

namespace Admin\Controller;
class RoleController extends BaseController {

    private $role_model;
    
    public function _initialize(){
		$this->role_model = getInstance('Admin\Model\RoleModel');
	}
	
	/**
	 * 角色列表页
	 */
	public function index(){
		$page = $this->view->page(count($this->role_model->where('form')->select()));
		$roles = $this->role_model->where('form')->order($page['orderFieldStr'])->limit("{$page['limit']},{$page['numPerPage']}")->select();
		
		$this->assign('roles', $roles);
		$this->assign('page', $page);
		$this->display('Admin/View/Role/index.php');
	}
	
	/**
	 * 添加角色
	 */
	public function add(){
		if(count($_POST) > 0){
			$datas = $this->getFormParams();
			$datas['create_time'] = date('Y-m-d H:i:s');
			if(!empty($datas['menu_ids'])){
				sort($datas['menu_ids']);
				$menu_ids = implode(',', $datas['menu_ids']);
				$datas['menu_ids'] = $menu_ids;	
			}else{
				$datas['menu_ids'] = '';
			}
				
			if($this->role_model->add($datas)){
				$this->ajaxReturn(200, '添加成功', '', 'closeCurrent');
			}else{
				$this->ajaxReturn(300, '添加失败');
			}
		}else{
			$this->assign('trees', $this->getTree());
			$this->display('Admin/View/Role/add.php');
		}
	}
	
	/**
	 * 修改角色
	 */
	public function update(){
		if(count($_POST) > 0){
			$datas = $this->getFormParams();
			$id = $datas['id'];
			unset($datas['id']);
			if(!empty($datas['menu_ids'])){
				sort($datas['menu_ids']);
				$menu_ids = implode(',', $datas['menu_ids']);
				$datas['menu_ids'] = $menu_ids;	
			}else{
				$datas['module_ids'] = '';
			}
			if($this->role_model->where('id='.$id)->update($datas)){
				$this->ajaxReturn(200, '修改成功', '', 'closeCurrent');
			}else{
				$this->ajaxReturn(300, '修改失败', '');
			}
		}else{
			$role_id = (int)$_GET['id'];
			$role_info = $this->role_model->where("id=$role_id")->find();
			$this->assign('trees', $this->getTree($role_info['menu_ids']));
			$this->assign('role_info', $role_info);
			$this->display('Admin/View/Role/update.php');
		}
	}
	
	
	
	/**
	 * 删除角色
	 */
	public function delete(){
		$ids = $_REQUEST['ids'];
		if(!empty($ids) && $this->role_model->where("id IN($ids)")->delete()){
			$this->ajaxReturn(200, '删除成功');
		}else{
			$this->ajaxReturn(300, '删除失败');
		}
	}
	
	    
    /**
     * 获取菜单树节点
     */
    protected function getTree($menu_ids=''){
    	if(!empty($menu_ids))
    		$role_menus = explode(',', $menu_ids);
		
    	function getChildNode($menus, $role_menus){
			$tree = '';
    		if(is_array($menus)){
	    		foreach($menus as $menu){
	    			$menu_id = $menu['id'];
	    			//$tree .= "<li><a tname=\"menu_ids[]\" tvalue=\"{$menu_id}\" checked=\"".($role_menus && in_array($menu_id, $role_menus) ? 'true' : 'false')."\">{$menu['name']}</a>";
	    			$tree .= '<li><a tname="menu_ids[]" tvalue="'.$menu_id.'" '.($role_menus && in_array($menu_id, $role_menus) ? 'checked="true"' : '').'>'.$menu['name'].'</a>';
	    			if(isset($menu['childs'])){
	    				$tree .= '<ul>';
	    				$tree .= getChildNode($menu['childs'], $role_menus);
	    				$tree .= '</ul>';	
	    			}
	    			$tree .= '</li>';
	    		}
	    	}
	    	return $tree;
    	}
    	
    	return getChildNode(getInstance('Admin\Model\MenuModel')->getMenu(), $role_menus);
    }
    
    
    /**
     * 查看角色配置文件
     */
    public function lookConfig(){
    	$role_id = (int)$_REQUEST['id'];
		$role_info = $this->role_model->field('config')->where("id=$role_id")->find();
		//$role_info['config'] = str_replace('&', '&amp;', htmlspecialchars_decode($role_info['config']));
		$this->assign('role_info', $role_info);
		$this->display();
    }

    
}
?>